<template>
    <div class="content">
        <div class="clearfix">
            <Form class="f-fl" inline :label-width="5">
                <FormItem>
                    <Input
                        v-model="params.topic"
                        placeholder="题干"
                    ></Input>
                </FormItem>
                <FormItem>
                    <Select v-model="params.type" placeholder="题型" style="width:100px" clearable>
                        <Option v-for="(item, k) in questionType" :key="k" :value="k">{{ item }}</Option>
                    </Select>
                </FormItem>
                <FormItem>
                    <Select v-model="params.difficulty" placeholder="难度" style="width:100px" clearable>
                        <Option v-for="(item, k) in questionDifficulty" :key="k" :value="k">{{ item }}</Option>
                    </Select>
                </FormItem>
                <FormItem>
                    <Select v-model="params.schoolId" @on-change="changeFrom" placeholder="来源" style="width:100px" clearable>
                        <Option v-for="(item, k) in schoolList" :key="k" :value="item.id">{{ item.name }}</Option>
                    </Select>
                </FormItem>
                <FormItem>
                    <Button type="primary" @click="search">搜索</Button>
                </FormItem>
                <FormItem>
                    <Button @click="clearSearch">清空</Button>
                </FormItem>
            </Form>
            <div class="f-fr">
                <Button type="primary" @click="handleAdd">新建试题</Button>
                <Button style="margin-left: 10px;" @click="handleExport">导入试题</Button>
                <Button style="margin-left: 10px;" @click="handleEx">导出试题</Button>
                <Button style="margin-left: 10px;" @click="handleAddSort">新建分类</Button>
            </div>
        </div>
        <Table @on-selection-change="getSelected" :data="list" :columns="columns" border align="center">

            <template slot-scope="{ row }" slot="sort">
                <div>{{ row.sort }}</div>
            </template>
            <template slot-scope="{ row }" slot="topic">
                <div>{{ row.topic }}</div>
            </template>
            <template slot-scope="{ row }" slot="type">
                <div>{{ questionType[row.type] }}</div>
            </template>
            <template slot-scope="{ row }" slot="difficulty">
                <div>{{ row.difficulty==1?'简单':row.difficulty==2?'一般':row.difficulty==3?'困难':'' }}</div>
            </template>
            <template slot-scope="{ row }" slot="score">
                <div>{{ row.score }}</div>
            </template>
            <template slot-scope="{ row }" slot="creator">
                <div>{{ row.creator }}</div>
            </template>
            <template slot-scope="{ row }" slot="createTime">
                <div>{{ row.createTime }}</div>
            </template>
            <template slot-scope="{ row }" slot="action">
                <div class="table-action">
                    <Button type="text" :disabled="($store.state.user.roleType==2&&row.roleType==1)||($store.state.user.roleType==1&&row.roleType==2)" @click="handleEdit(row.testId)">编辑</Button>
                    <Button type="text" :disabled="($store.state.user.roleType==2&&row.roleType==1)||($store.state.user.roleType==1&&row.roleType==2)" @click="handleDelete(row.testId)">删除</Button>
                </div>
            </template>
        </Table>
        <Pagination
            :pageSize.sync="params.pageSize"
            :current.sync="params.pageNum"
            :total="params.total"
            @change="queryList"
        />
    </div>
</template>
<script>
import { questionType, questionDifficulty } from '@/libs/enums'
import { schoolManageList } from '@/api/schoolManage'
import { questionList, delQuestion } from '@/api/questionManage'
import { downloadQues } from '@/api/fileManage'
export default {
    data () {
        return {
            selectedData: [],
            schoolList: [],
            questionType,
            questionDifficulty,
            params: {
                total: 0,
                pageNum: 1,
                pageSize: 10,
                schoolName: '',
                type: '', // 题型
                difficulty: '', // 难度
                schoolId: '',
                onlySystem: '',
                topic: ''
            },
            list: [],
            columns: Object.freeze([
                {
                    type: 'selection',
                    width: 60,
                    align: 'center'
                },
                { title: '序号', type: 'index', align: 'center', width: 80 },
                { title: '所属分类', slot: 'sort', align: 'center' },
                { title: '题干', slot: 'topic', align: 'center' },
                { title: '题型', slot: 'type', align: 'center' },
                { title: '难度', slot: 'difficulty', align: 'center' },
                { title: '分值', slot: 'score', align: 'center' },
                { title: '来源', slot: 'creator', align: 'center' },
                { title: '创建时间', slot: 'createTime', align: 'center' },
                { title: '操作', slot: 'action', align: 'center', width: 100 }
            ])
        }
    },
    inject: ['nextPageReload'],
    mounted () {
        this.getSchoolList()
        this.queryList()
    },
    methods: {
        search () {
            this.params.pageNum = 1
            this.queryList()
        },
        getSelected (data) {
            console.log(data)
            this.selectedData = data
        },
        handleEx () {
            if (this.selectedData.length > 0) {
                let arr = this.selectedData.map(item => { return item.testId })
                let str = arr.join(',')
                // this.selectedData.map(item => {
                downloadQues(str, '/testManage/export')
                // })
            } else {
                this.$Message.error('请勾选')
            }
        },
        changeFrom (id) {
            console.log(id)
            if (id == 1) {
                this.params.schoolId = ''
                this.params.onlySystem = 1
            } else {
                this.params.onlySystem = ''
            }
        },
        getSchoolList () {
            var obj = { 'total': 0, 'status': '', 'pageNum': 1, 'pageSize': 10, 'schoolName': '', 'enable': '' }
            schoolManageList(obj).then(res => {
                console.log(res.data.data.list)
                this.schoolList = res.data.data.list.map(item => {
                    return {
                        id: item.schoolId,
                        name: item.schoolName
                    }
                })
                this.schoolList.unshift({ id: 1, name: '系统后台' })
            })
        },
        queryList () {
            /* this.list = Array(10).fill(0).map((val, i) => {
                return {
                    schoolId: i,
                    schoolName: `教师账号${i}`,
                    contacts: `教师姓名${i}`,
                    tel: `联系方式${i}`,
                    createTime: `所属专业${i}`,
                    field1: `一般${i}`,
                    field2: i + 1
                }
            }) */
            let params = {
                difficulty: this.params.difficulty, // 难度
                onlySystem: this.params.onlySystem, // 只查询系统1
                pageNum: this.params.pageNum,
                pageSize: this.params.pageSize,
                schoolId: this.params.schoolId, // 为空则查询所有学校和系统
                sortId: '', // 分类ID
                topic: this.params.topic, // 题干
                type: this.params.type// 题型
            }
            // 获取试题列表
            questionList(params).then((res) => {
                console.log(res)
                this.list = res.data.data.list
                this.params.pageNum = res.data.data.pageNum
                this.params.pageSize = res.data.data.pageSize
                this.params.total = res.data.data.total
            })
        },
        clearSearch () {
            this.params.topic = ''
            this.params.difficulty = ''
            this.params.type = ''
            this.params.schoolId = ''
        },
        handleAdd () {
            this.nextPageReload()
            this.$router.push({
                name: 'testQuestionsAdd'
            })
        },
        handleEdit (id) {
            this.nextPageReload()
            this.$router.push({
                name: 'testQuestionsEdit',
                params: {
                    id
                }
            })
        },
        handleDelete (id) {
            this.$Modal.confirm({
                title: '提示',
                content: '确认是否删除？',
                onOk: () => {
                    let params = {
                        testId: id
                    }
                    delQuestion(params).then((res) => {
                        console.log(res)
                        this.queryList()
                    })
                }
            })
        },
        handleExport () {
            this.nextPageReload()
            this.$router.push({ name: 'testQuestionsExport' })
        },
        handleAddSort () {
            this.$router.push({ name: 'testQuestionsSort' })
        }
    }
}
</script>
